//
//  TopicDetailHeader.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/8.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "TopicDetailHeader.h"
#import "TopicObj.h"
#import "BeePhotoImageView.h"
#import "UrlLabel.h"
#import "LargeTitle.h"
#import "FbeeCommitBtn.h"

@interface TopicDetailHeader()
{
    //后期可能需要换成detail Obj
    TopicObj *topicObj;
    
    UILabel *largeTitle;
    
    BeePhotoImageView *photo;
    UILabel *nickName;
    UrlLabel *content;
    UILabel *dateInfo;
    FbeeCommitBtn *shareBtn;
    UILabel *commentInfo;
    
}
@end

@implementation TopicDetailHeader

-(instancetype)initWithObj:(TopicObj *)obj
{
    self = [super initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, 0)];
    
    if (self) {
        topicObj = obj;
        [self buildView];
    }
    
    return self;
}

-(void)buildView
{
    largeTitle = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.setFrame(CGRectMake(16, 0, IPHONEWIDTH-16, 50));
        make.addText(NSLocalizedString(@"tNavBeeDetail", nil));
        make.addFont([UIFont boldSystemFontOfSize:34.0f]).addTextColor(FBEE_COLOR_LARGETITLE);
        [self addSubview:make];
    }];
    
    [self addSubview:largeTitle];
    
    photo = [[BeePhotoImageView alloc] initWithFrame:CGRectMake(15, largeTitle.bottom + 19, 35, 35) imgByTitle:topicObj.nickname photoStyle:BeePhotoCellStyle];
    [self addSubview:photo];
    
    nickName = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addTextColor(FBEE_COLOR_THEME).addFont([UIFont systemFontOfSize:18.0f]);
        make.addText(topicObj.nickname);
        make.setFrame(CGRectMake(0, 0, 18*15+10, 23));
        make.left = photo.right + 10;
        make.top = photo.top - 1;
        [self addSubview:make];
    }];
    
    dateInfo = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addTextColor(FBEE_COLOR_INFO).addFont([UIFont systemFontOfSize:12]);
        make.addText(topicObj.createTime);
        make.setFrame(CGRectMake(0, 0, 150, 13));
        make.left = photo.right + 10;
        make.top = nickName.bottom;
        [self addSubview:make];
    }];
    
    content = [[UrlLabel alloc] initWithFrame:CGRectMake(16, photo.bottom + 20, IPHONEWIDTH - 16 - 15, 2000*15)];
    content.numberOfLines = 2000;
    [content setText:topicObj.content textColor:FBEE_COLOR_TEXT font:[UIFont systemFontOfSize:18.0f] urlColor:FBEE_COLOR_BLUE urlTapBlock:^(NSURL *url) {

        UIApplication *application = [UIApplication sharedApplication];
        if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
            [application openURL:url options:@{} completionHandler:nil];
        } else {
            [application openURL:url];
        }

    }];
    [content sizeToFit];

    if (content.height < 20 ) {
        content.height = 20;
    }
    
    [self addSubview:content];
    

    
    shareBtn = [[FbeeCommitBtn alloc] initWithTitle:NSLocalizedString(@"mShare", nil)];
    [shareBtn setBtnFrame:CGRectMake(0, content.bottom + 40, 115, 50)];
    shareBtn.centerX = IPHONEWIDTH/2;
    [shareBtn onClick:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Share object:nil];
    }];
    [self addSubview:shareBtn];
    
    commentInfo = [UILabel makeLabel:^(TTLabelExtend *make) {
        
        NSString *text = [NSString stringWithFormat:@"%@  (%@)",NSLocalizedString(@"mComment", nil),[topicObj.commentNum isEqualToString:NSLocalizedString(@"mComment", nil)] ? @"0" : topicObj.commentNum];
        make.addText(text);
        make.addTextColor(FBEE_COLOR_THEME).addFont([UIFont systemFontOfSize:18.0f]);
        make.setFrame(CGRectZero);
        [make sizeToFit];
        make.height = 20;
        make.top = shareBtn.bottom + 10;
        make.left = 15;
        [self addSubview:make];
    }];
    
    
    self.height = commentInfo.bottom;
    
}

-(void)refreshView
{
    commentInfo.text = [NSString stringWithFormat:@"%@  (%@)",NSLocalizedString(@"mComment", nil),[topicObj.commentNum isEqualToString:NSLocalizedString(@"mComment", nil)] ? @"0" : topicObj.commentNum];\
    [commentInfo sizeToFit];
    commentInfo.height = 20;
}

@end
